<template>
	<view class="updatePhone">
		<view class="mobile">
			<view class="phoneNumber">
				手机号
			</view>
			<input type="number" v-model="phone" maxlength="11" placeholder="请输入手机号" />
		</view>
		<view class="mobile">
			<view class="phoneNumber">
				验证码
			</view>
			<input type="number" style="width: 40vw;" v-model="code" placeholder="请输入验证码" />
			<view v-if="!temp" class="code" @click="getNote">获取验证码</view>
			<view v-else style="color: #007AFF;">{{time}}s</view>
		</view>
		<view class="enterPhone" @click="savePhone">确认新手机号</view>
	</view>
</template>

<script>
	import Dialog from '../../../wxcomponents/vant-weapp/dialog/dialog'
	import {
		noteLogin
	} from "../../../api/login.js"
	export default {
		data() {
			return {
				phone: undefined,
				code: undefined,
				temp: false,
				time:60,
			}
		},
		methods: {
			savePhone() {
				if (!this.phone || this.phone < 11) {
					Dialog({
						title: "系统提示",
						message: "请填写正确手机号",
					})
					let self = this
					noteLogin(this.user).then(res => {
						uni.hideLoading();
						self.temp = true
						let timekeeping = setInterval(
							function() {
								self.time -= 1
								if (self.time == 0) {
									self.time = 60
									self.temp = false
									window.clearInterval(timekeeping)
								}
							}, 1000)
					})
				} else if (!this.code || this.code < 6) {
					Dialog({
						title: "系统提示",
						message: "验证码有误",
					})
				}
			},
			getNote() {
				let self = this
				noteLogin(this.user).then(res => {
					uni.hideLoading();
					self.temp = true
					let timekeeping = setInterval(
						function() {
							self.time -= 1
							if (self.time == 0) {
								self.time = 60
								self.temp = false
								window.clearInterval(timekeeping)
							}
						}, 1000)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.updatePhone {
		background: #d4d4d4;
		height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;

		.mobile {
			font-size: 30rpx;
			display: flex;
			background: #FFFFFF;
			width: 100vw;
			padding: 10rpx 0;
			border-bottom: solid 1px #cccccc;

			.phoneNumber {
				text-align: center;
				width: 30vw;
			}

			input {
				width: 50vw;
			}

			.code {
				margin-right: 20rpx;
				background: #007AFF;
				color: #FFFFFF;
				padding: 10rpx 6rpx;
				border-radius: 10rpx;
			}
		}

		.enterPhone {
			margin-top: 20rpx;
			background: #007AFF;
			color: #FFFFFF;
			font-size: 32rpx;
			padding: 20rpx 120rpx;
			width: 90vw;
			text-align: center;
			border-radius: 20rpx;
		}
	}
</style>
